package com.company.project.controller.goods;

import com.company.project.service.HttpSessionService;
import com.company.project.vo.req.goods.GoodsInsterReqVO;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;

import com.company.project.common.utils.DataResult;

import com.company.project.entity.SysGoodsEntity;
import com.company.project.service.SysGoodsService;


/**
 * @author YangYulin
 * @email YangYulinit@163.com
 * @date 2020-08-05 16:20:50
 */
@Controller
@RequestMapping("/")
@Api(tags = "商品管理-商品信息")
public class SysGoodsController {
    @Autowired
    private SysGoodsService sysGoodsService;


    @Autowired
    private HttpSessionService httpSessionService;

    /**
     * 跳转到页面
     */
    @GetMapping("/index/sysGoods")
    public String sysGoods() {
        return "sysgoods/list";
    }

    @ApiOperation(value = "新增")
    @PostMapping("sysGoods/add")
    @RequiresPermissions("sysGoods:add")
    @ResponseBody
    public DataResult add(@RequestBody GoodsInsterReqVO vo) {
//        sysGoodsService.save(sysGoods);
        sysGoodsService.insterGoods(vo, httpSessionService.getCurrentStoreId());
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("sysGoods/delete")
    @RequiresPermissions("sysGoods:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
        sysGoodsService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("sysGoods/update")
    @RequiresPermissions("sysGoods:update")
    @ResponseBody
    public DataResult update(@RequestBody SysGoodsEntity sysGoods) {
        sysGoodsService.updateById(sysGoods);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("sysGoods/listByPage")
    @RequiresPermissions("sysGoods:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody SysGoodsEntity sysGoods) {
        Page page = new Page(sysGoods.getPage(), sysGoods.getLimit());
        QueryWrapper queryWrapper = new QueryWrapper();
        //查询条件示例
        //queryWrapper.eq("id", sysGoods.getId());
        IPage<SysGoodsEntity> iPage = sysGoodsService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

}
